﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeavenBAL;
using FortuneHeavenCommon.Enum;
using FortuneHeaven.Admin.Common;
using FortuneHeavenCommon.DataMap;


namespace FortuneHeaven.Admin.Pages.CMS
{
    public partial class NewsDetail : FortuneHeaven.Admin.Common.PageBase
    {
        public string showtitle;
        public string content;
        private string pre = "";

        public override string PageID
        {
            get
            {
                if (Type >= 0)
                {
                    NewsType newstype = (NewsType)Type;
                    switch (newstype)
                    {
                        case NewsType.BroadCast:
                            return "F11";
                        case NewsType.Games:
                            return "F12";
                        case NewsType.Competition:
                            return "F13";
                        default:
                            break;
                    }
                }
                return "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                if (ID >= 0)
                {
                    NewsBO bo = new NewsBO();
                    News g = bo.GetInfo(ID);
                    
                    this.txtTitle.Text = g.Title;
                    content = g.Content;
                }
                
                if (Type>=0)
                {
                    NewsType newstype = (NewsType)Type;
                    if (ID<0)
                    {
                        switch (newstype)
                        {
                            case NewsType.BroadCast:
                                showtitle = "新增新闻公告";
                                break;
                            case NewsType.Games:
                                showtitle = "新增游戏公告";
                                break;
                            case NewsType.Competition:
                                showtitle = "新增赛事公告";
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {
                        switch (newstype)
                        {
                            case NewsType.BroadCast:
                                showtitle = "修改新闻公告";
                                break;
                            case NewsType.Games:
                                showtitle = "修改游戏公告";
                                break;
                            case NewsType.Competition:
                                showtitle = "修改赛事公告";
                                break;
                            default:
                                break;
                        }
                    }
                   
                }                
            }
        }
        public int Type
        {
            get
            {
                return Request["Type"] == null ? -1 : int.Parse(Request["Type"]);
            }

        }


        public new int? ID
        {
            get
            {
                return string.IsNullOrEmpty(Request["ID"]) ? -1 : int.Parse(Request["ID"]);
            }
        }

        public string Pre
        {
            get
            {
                if (!string.IsNullOrEmpty(pre))
                    return pre;
                else
                    pre = Server.UrlDecode(Request["pre"]);
                return pre;
            }
        }

        public void Save(object sender, EventArgs arg)
        {
            if (!this.IsValid)
            {
                return;
            }
            NewsBO bo = new NewsBO();
            News g;
            if (ID >= 0)
            {
                g = bo.GetInfo(ID);

            }
            else
            {
                g = new News();
                g.Time = System.DateTime.Now;
            }
            g.Title = this.txtTitle.Text;
            g.NewsType = Type;
            g.Content = Request.Form["content"];
            
            if (ID >= 0)
            {
                bo.Save(g, false);
                Response.Write(string.Format("<Html> <script>alert('修改成功');document.location ='{0}'; </script> </HTML>", Pre));
            }
            else
            {
                g.ID = bo.GetMaxID();
                g.SubmitUser = AuthorityCenter.CurrentUserID;
                bo.Save(g, true);
                Response.Write(string.Format("<Html> <script>alert('新增成功');document.location ='{0}'; </script> </HTML>", Pre));
                Response.Flush();
            }
        }
        public void CancelSave(object sender, EventArgs arg)
        {
            Response.Redirect(Pre);
        }
    }
}